package org.xwalk.core.internal;

import android.util.Log;
import java.net.MalformedURLException;
import java.net.URL;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class XWalkCookieManagerBridge extends XWalkCookieManagerInternal {
    private XWalkCoreBridge coreBridge;
    private Object wrapper;

    public Object getWrapper() {
        return wrapper;
    }



    public XWalkCookieManagerBridge(Object wrapper) {
        this.wrapper = wrapper;
        reflectionInit();
    }

    @Override
    public void setAcceptCookie(boolean accept) {
        if (setAcceptCookiebooleanMethod == null || setAcceptCookiebooleanMethod.isNull()) {
            setAcceptCookieSuper(accept);
        } else {
            setAcceptCookiebooleanMethod.invoke(accept);
        }
    }

    public void setAcceptCookieSuper(boolean accept) {
        super.setAcceptCookie(accept);
    }

    private ReflectMethod setAcceptCookiebooleanMethod = new ReflectMethod(null, "setAcceptCookie");

    @Override
    public boolean acceptCookie() {
        if (acceptCookieMethod == null || acceptCookieMethod.isNull()) {
            return acceptCookieSuper();
        } else {
            return (Boolean)acceptCookieMethod.invoke();
        }
    }

    public boolean acceptCookieSuper() {
        boolean ret;
        ret = super.acceptCookie();
        
        return ret;
    }

    private ReflectMethod acceptCookieMethod = new ReflectMethod(null, "acceptCookie");

    @Override
    public void setCookie(final String url, final String value) {
        if (setCookieStringStringMethod == null || setCookieStringStringMethod.isNull()) {
            setCookieSuper(url, value);
        } else {
            setCookieStringStringMethod.invoke(url, value);
        }
    }

    public void setCookieSuper(final String url, final String value) {
        super.setCookie(url, value);
    }

    private ReflectMethod setCookieStringStringMethod = new ReflectMethod(null, "setCookie");

    @Override
    public String getCookie(final String url) {
        if (getCookieStringMethod == null || getCookieStringMethod.isNull()) {
            return getCookieSuper(url);
        } else {
            return (String)getCookieStringMethod.invoke(url);
        }
    }

    public String getCookieSuper(final String url) {
        String ret;
        ret = super.getCookie(url);
        if (ret == null) return null;
        return ret;
    }

    private ReflectMethod getCookieStringMethod = new ReflectMethod(null, "getCookie");

    @Override
    public void removeSessionCookie() {
        if (removeSessionCookieMethod == null || removeSessionCookieMethod.isNull()) {
            removeSessionCookieSuper();
        } else {
            removeSessionCookieMethod.invoke();
        }
    }

    public void removeSessionCookieSuper() {
        super.removeSessionCookie();
    }

    private ReflectMethod removeSessionCookieMethod = new ReflectMethod(null, "removeSessionCookie");

    @Override
    public void removeAllCookie() {
        if (removeAllCookieMethod == null || removeAllCookieMethod.isNull()) {
            removeAllCookieSuper();
        } else {
            removeAllCookieMethod.invoke();
        }
    }

    public void removeAllCookieSuper() {
        super.removeAllCookie();
    }

    private ReflectMethod removeAllCookieMethod = new ReflectMethod(null, "removeAllCookie");

    @Override
    public boolean hasCookies() {
        if (hasCookiesMethod == null || hasCookiesMethod.isNull()) {
            return hasCookiesSuper();
        } else {
            return (Boolean)hasCookiesMethod.invoke();
        }
    }

    public boolean hasCookiesSuper() {
        boolean ret;
        ret = super.hasCookies();
        
        return ret;
    }

    private ReflectMethod hasCookiesMethod = new ReflectMethod(null, "hasCookies");

    @Override
    public void removeExpiredCookie() {
        if (removeExpiredCookieMethod == null || removeExpiredCookieMethod.isNull()) {
            removeExpiredCookieSuper();
        } else {
            removeExpiredCookieMethod.invoke();
        }
    }

    public void removeExpiredCookieSuper() {
        super.removeExpiredCookie();
    }

    private ReflectMethod removeExpiredCookieMethod = new ReflectMethod(null, "removeExpiredCookie");

    @Override
    public void flushCookieStore() {
        if (flushCookieStoreMethod == null || flushCookieStoreMethod.isNull()) {
            flushCookieStoreSuper();
        } else {
            flushCookieStoreMethod.invoke();
        }
    }

    public void flushCookieStoreSuper() {
        super.flushCookieStore();
    }

    private ReflectMethod flushCookieStoreMethod = new ReflectMethod(null, "flushCookieStore");

    @Override
    public boolean allowFileSchemeCookies() {
        if (allowFileSchemeCookiesMethod == null || allowFileSchemeCookiesMethod.isNull()) {
            return allowFileSchemeCookiesSuper();
        } else {
            return (Boolean)allowFileSchemeCookiesMethod.invoke();
        }
    }

    public boolean allowFileSchemeCookiesSuper() {
        boolean ret;
        ret = super.allowFileSchemeCookies();
        
        return ret;
    }

    private ReflectMethod allowFileSchemeCookiesMethod = new ReflectMethod(null, "allowFileSchemeCookies");

    @Override
    public void setAcceptFileSchemeCookies(boolean accept) {
        if (setAcceptFileSchemeCookiesbooleanMethod == null || setAcceptFileSchemeCookiesbooleanMethod.isNull()) {
            setAcceptFileSchemeCookiesSuper(accept);
        } else {
            setAcceptFileSchemeCookiesbooleanMethod.invoke(accept);
        }
    }

    public void setAcceptFileSchemeCookiesSuper(boolean accept) {
        super.setAcceptFileSchemeCookies(accept);
    }

    private ReflectMethod setAcceptFileSchemeCookiesbooleanMethod = new ReflectMethod(null, "setAcceptFileSchemeCookies");


    void reflectionInit() {
        coreBridge = XWalkCoreBridge.getInstance();
        if (coreBridge == null) return;

        setAcceptCookiebooleanMethod.init(wrapper, null,
                "setAcceptCookie", boolean.class);
        acceptCookieMethod.init(wrapper, null,
                "acceptCookie");
        setCookieStringStringMethod.init(wrapper, null,
                "setCookie", String.class, String.class);
        getCookieStringMethod.init(wrapper, null,
                "getCookie", String.class);
        removeSessionCookieMethod.init(wrapper, null,
                "removeSessionCookie");
        removeAllCookieMethod.init(wrapper, null,
                "removeAllCookie");
        hasCookiesMethod.init(wrapper, null,
                "hasCookies");
        removeExpiredCookieMethod.init(wrapper, null,
                "removeExpiredCookie");
        flushCookieStoreMethod.init(wrapper, null,
                "flushCookieStore");
        allowFileSchemeCookiesMethod.init(wrapper, null,
                "allowFileSchemeCookies");
        setAcceptFileSchemeCookiesbooleanMethod.init(wrapper, null,
                "setAcceptFileSchemeCookies", boolean.class);
    }
}
